package br.com.lh.urabuilder.view.properties
{
	import br.com.lh.urabuilder.domain.xml.ConditionalAction;
	import br.com.lh.urabuilder.domain.xml.Option;
	import br.com.lh.urabuilder.util.SelectableComponentsUtils;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Grid;
	import mx.containers.GridItem;
	import mx.containers.GridRow;
	import mx.controls.Image;
	import mx.controls.Label;
	
	public class OptionGrid extends Grid
	{
		private var conditional:ConditionalAction;
		public var optionList:ArrayCollection = new ArrayCollection();
		
		
		public function OptionGrid(conditional:ConditionalAction)	{
			this.conditional = conditional;
			var elementList:ArrayCollection = conditional.getOptions();
			createHeaderRow();
			for each (var option:Option in elementList){
				if (option !=null){
					doAdd(option);
				}
			}
		}
		
		private function deleteRow(evt:MouseEvent,gridRow:GridRow,option:Option):void{
			this.removeChild(gridRow);
			var index:int = optionList.getItemIndex(option);
			optionList.removeItemAt(index);
		}
		
		
		private function createHeaderRow():void{
			var row:GridRow = new GridRow();
			var item:GridItem = new GridItem();
			var label:Label = new Label();
			label.text="pattern";
			item.addChild(label);
			row.addChild(item);
			
			item= new GridItem();
			label= new Label();
			label.text="action";
			item.addChild(label);
			row.addChild(item);
			
			item= new GridItem();
			var img:Image= new Image();
			img.source ="icons/action_add.gif";
			img.buttonMode = true;
			img.addEventListener(MouseEvent.MOUSE_DOWN,addOption);
			item.addChild(img);
			row.addChild(item);
			this.addChild(row);
		}
		
		private function addOption(evt:MouseEvent):void{
			doAdd(new Option());
		}
		
		private function doAdd(option:Option):void{
			optionList.addItem(option);
			var optionLine:OptionGridRow = new OptionGridRow(option);
			optionLine.deleteImage.addEventListener(MouseEvent.MOUSE_DOWN,function(e:MouseEvent):void {
					deleteRow(e,optionLine,option);});
			this.addChild(optionLine);
		}

	}
}